/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.close.basic.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.hopes.bizz.close.basic.entity.ClosedGateEntity;
import com.hopes.bizz.close.basic.dto.ClosedGateExcelDTO;
import com.hopes.bizz.close.basic.vo.ClosedGateExcelVO;
import com.hopes.bizz.close.basic.mapper.ClosedGateMapper;
import com.hopes.bizz.close.basic.service.ClosedGateService;
import com.hopes.bizz.close.common.constant.BasicDicConstant;
import com.hopes.bizz.close.common.constant.enums.ParkCloseSysDictEnum;
import com.hopes.bizz.common.core.util.DateUtils;
import com.hopes.bizz.common.core.validate.ValidatorUtils;
import com.hopes.bizz.video.common.constant.enums.VideoModDictEnum;
import com.hopes.boss.admin.api.entity.SysDictItem;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.admin.api.resolver.enums.SysDictEnum;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/**
 * 出入园卡口信息
 *
 * @author pig
 * @date 2023-10-20 16:01:48
 */
@Service
public class ClosedGateServiceImpl extends ServiceImpl<ClosedGateMapper, ClosedGateEntity> implements ClosedGateService {

    @Override
    public boolean save(ClosedGateEntity closedGateEntity) {
        closedGateEntity.setNetworkStatus(BasicDicConstant.CLOSE_DEVICE_NETWORK_STATUS_NORMAL);
        return SqlHelper.retBool(getBaseMapper().insert(closedGateEntity));
    }

    @Override
    public ValidateImportBO<ClosedGateExcelDTO> validateImport(List<ClosedGateExcelDTO> excelDTOList) {
        ValidateImportBO<ClosedGateExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<ClosedGateExcelDTO> saveList = new ArrayList<>();
        int successNum = 0;
        int failedNum = 0;

        for (ClosedGateExcelDTO closedGateExcelDTO : excelDTOList) {
            final String validateRes = ValidatorUtils.importValidateEntity(closedGateExcelDTO);
            if (StrUtil.isEmpty(validateRes)) {
                successNum++;
                saveList.add(closedGateExcelDTO);
            } else {
                failedNum++;
                errorMessageList.add(new ErrorMessage(closedGateExcelDTO.getLineNum(), new HashSet<>() {
                    private static final long serialVersionUID = 2527128778922157091L;
                    {
                        add(validateRes);
                    }
                }));
            }
        }

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public ClosedGateEntity importDtoToEntity(ClosedGateExcelDTO excelDto) {
        ClosedGateEntity entity= new ClosedGateEntity();
        BeanUtils.copyProperties(excelDto, entity);
        entity.setNetworkStatus(BasicDicConstant.CLOSE_DEVICE_NETWORK_STATUS_NORMAL);
        return entity;
    }

    @Override
    public ClosedGateExcelVO exportEntityToVo(ClosedGateEntity entity) {
        ClosedGateExcelVO excelVO = new ClosedGateExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        return excelVO;
    }
}
